﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.OleDb;
using IDAL;
namespace AccessDAL {
    public partial class User : IUser{
        public User() { }
        #region Method
        /// <summary>
        /// 登录判断
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <returns></returns>
        public bool Login(string userName,string userPassword) {
            StringBuilder sb = new StringBuilder();
            sb.Append("select count(*) from [user]");
            sb.Append(" where username = @username and password = @password");
            OleDbParameter[] parameters = {
                new OleDbParameter("@username",OleDbType.VarChar,50),
                new OleDbParameter("@password",OleDbType.VarChar,50)
            };
            parameters[0].Value = userName;
            parameters[1].Value = userPassword;
            int n = (int)OleDbHelper.ExecuteScalar(sb.ToString(), parameters);
            if (n == 1) 
                return true;
            else
                return false;
        }
        /// <summary>
        /// 更新用户密码
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Update (Model.User model) {
            StringBuilder sb = new StringBuilder();
            sb.Append("update [user] set ");
            sb.Append(" [password] = @password");
            sb.Append(" where [username] = @username");
            OleDbParameter[] parameters = {
                new OleDbParameter("@password",OleDbType.VarChar,50),
                new OleDbParameter("@username",OleDbType.VarChar,50)
            };
            parameters[0].Value = model.Password;
            parameters[1].Value = model.UserName;
            int rows = OleDbHelper.ExecuteNonQuery(sb.ToString(), parameters);
            if (rows == 1)
                return true;
            else
                return false;
        }
        #endregion Method
    }
}
